﻿using CommandProcessor.Command;
using Core.Common;
using Data.Infrastructure;
using Data.Repositories;
using Domain.Commands;
using Domain.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Models;

namespace Domain.Handlers
{
    //Dung cho du lieu tham chieu
    //Dung de check add
    public class CanAddWorkplace : IValidationHandler<CreateOrUpdateWorkplaceCommand>
    {
        private readonly IWorkplaceRepository WorkplaceRepository;
        public CanAddWorkplace(IWorkplaceRepository WorkplaceRepository, IUnitOfWork unitOfWork)
        {
            this.WorkplaceRepository = WorkplaceRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateWorkplaceCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Name == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateWorkplaceCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateWorkplaceHandler : ICommandHandler<CreateOrUpdateWorkplaceCommand>
    {
        private readonly IWorkplaceRepository WorkplaceRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateWorkplaceHandler(IWorkplaceRepository WorkplaceRepository, IUnitOfWork unitOfWork)
        {
            this.WorkplaceRepository = WorkplaceRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateWorkplaceCommand command)
        {
            var Workplace = new Workplace
            {
                ID = command.ID,
                Description = command.Description,
                Name = command.Name,
                Code = command.Code,
                Function = command.Function,
                
            };
            if (Workplace.ID == 0)
                WorkplaceRepository.Add(Workplace);
            else
                WorkplaceRepository.Update(Workplace);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateWorkplaceCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateWorkplaceCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteWorkplaceHandler : ICommandHandler<DeleteWorkplaceCommand>
    {
        private readonly IWorkplaceRepository WorkplaceRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteWorkplaceHandler(IWorkplaceRepository WorkplaceRepository, IUnitOfWork unitOfWork)
        {
            this.WorkplaceRepository = WorkplaceRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteWorkplaceCommand command)
        {
            var Workplace = WorkplaceRepository.GetById(command.ID);
            WorkplaceRepository.Delete(Workplace);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteWorkplaceCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            WorkplaceRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteWorkplaceCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Unit
    public class CanAddUnit : IValidationHandler<CreateOrUpdateUnitCommand>
    {
        private readonly IUnitRepository UnitRepository;
        public CanAddUnit(IUnitRepository UnitRepository, IUnitOfWork unitOfWork)
        {
            this.UnitRepository = UnitRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateUnitCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Name == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateUnitCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateUnitHandler : ICommandHandler<CreateOrUpdateUnitCommand>
    {
        private readonly IUnitRepository UnitRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateUnitHandler(IUnitRepository UnitRepository, IUnitOfWork unitOfWork)
        {
            this.UnitRepository = UnitRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateUnitCommand command)
        {
            var Unit = new Unit
            {
                ID = command.ID,
                Description = command.Description,
                Name = command.Name,
                Code = command.Code,
            };
            if (Unit.ID == 0)
                UnitRepository.Add(Unit);
            else
                UnitRepository.Update(Unit);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateUnitCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateUnitCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteUnitHandler : ICommandHandler<DeleteUnitCommand>
    {
        private readonly IUnitRepository UnitRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteUnitHandler(IUnitRepository UnitRepository, IUnitOfWork unitOfWork)
        {
            this.UnitRepository = UnitRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteUnitCommand command)
        {
            var Unit = UnitRepository.GetById(command.ID);
            UnitRepository.Delete(Unit);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteUnitCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            UnitRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteUnitCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Employee

    public class CanAddEmployee: IValidationHandler<CreateOrUpdateEmployeeCommand>
    {
        private readonly IEmployeeRepository EmployeeRepository;
        public CanAddEmployee(IEmployeeRepository EmployeeRepository, IUnitOfWork unitOfWork)
        {
            this.EmployeeRepository = EmployeeRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateEmployeeCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Name == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateEmployeeCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateEmployeeHandler : ICommandHandler<CreateOrUpdateEmployeeCommand>
    {
        private readonly IEmployeeRepository EmployeeRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateEmployeeHandler(IEmployeeRepository EmployeeRepository, IUnitOfWork unitOfWork)
        {
            this.EmployeeRepository = EmployeeRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateEmployeeCommand command)
        {
            var Employee = new Employee
            {
                ID = command.ID,
                Description = command.Description,
                Name = command.Name,
                Code = command.Code,
                Function = command.Function,
                LevelID=command.LevelID,
                UnitID=command.UnitID,
                Expertise=command.Expertise,

            };
            if (Employee.ID == 0)
                EmployeeRepository.Add(Employee);
            else
                EmployeeRepository.Update(Employee);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateEmployeeCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateEmployeeCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteEmployeeHandler : ICommandHandler<DeleteEmployeeCommand>
    {
        private readonly IEmployeeRepository EmployeeRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteEmployeeHandler(IEmployeeRepository EmployeeRepository, IUnitOfWork unitOfWork)
        {
            this.EmployeeRepository = EmployeeRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteEmployeeCommand command)
        {
            var Employee = EmployeeRepository.GetById(command.ID);
            EmployeeRepository.Delete(Employee);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteEmployeeCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            EmployeeRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteEmployeeCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //*Dung cho lenh san xuat
    // Order
    public class CanAddOrder : IValidationHandler<CreateOrUpdateOrderCommand>
    {
        private readonly IOrderRepository OrderRepository;
        public CanAddOrder(IOrderRepository OrderRepository, IUnitOfWork unitOfWork)
        {
            this.OrderRepository = OrderRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateOrderCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateOrderCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateOrderHandler : ICommandHandler<CreateOrUpdateOrderCommand>
    {
        private readonly IOrderRepository OrderRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateOrderHandler(IOrderRepository OrderRepository, IUnitOfWork unitOfWork)
        {
            this.OrderRepository = OrderRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateOrderCommand command)
        {
            var Order = new Order
            {
                ID = command.ID,
                User1ID=command.User1ID,
                User2ID=command.User2ID,
                Date1=command.Date1,
                Date2=command.Date2,
                StartDate=command.StartDate,
                EndDate=command.EndDate,
                ProductModelID=command.ProductModelID,
                Quantity=command.Quantity,
                Note=command.Note,
                Code = command.Code,
                Status=command.Status,
                TotalCost=command.TotalCost
            };
            if (Order.ID == 0)
                OrderRepository.Add(Order);
            else
                OrderRepository.Update(Order);

            unitOfWork.Commit();
            return new CommandResult(true,Order.ID);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateOrderCommand> commands)
        {
            List<Order> list = new List<Order>();
            foreach (var item in commands)
            {
                var Order = new Order
                {
                    ID = item.ID,
                    User1ID = item.User1ID,
                    User2ID = item.User2ID,
                    Date1 = item.Date1,
                    Date2 = item.Date2,
                    StartDate = item.StartDate,
                    EndDate = item.EndDate,
                    ProductModelID = item.ProductModelID,
                    Quantity = item.Quantity,
                    Note = item.Note,
                    Code = item.Code,
                    Status = item.Status,
                    TotalCost = item.TotalCost

                };
                list.Add(Order);
            }
            if (list.Any(x => x.ID == 0))
                OrderRepository.AddRange(list.Where(x => x.ID == 0));
            else
                OrderRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateOrderCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteOrderHandler : ICommandHandler<DeleteOrderCommand>
    {
        private readonly IOrderRepository OrderRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteOrderHandler(IOrderRepository OrderRepository, IUnitOfWork unitOfWork)
        {
            this.OrderRepository = OrderRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteOrderCommand command)
        {
            var Order = OrderRepository.GetById(command.ID);
            OrderRepository.Delete(Order);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteOrderCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            OrderRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteOrderCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //MaterialDetail
    //Dung de check add
    public class CanAddMaterialDetail : IValidationHandler<CreateOrUpdateMaterialDetailCommand>
    {
        private readonly IMaterialDetailRepository MaterialDetailRepository;
        public CanAddMaterialDetail(IMaterialDetailRepository MaterialDetailRepository, IUnitOfWork unitOfWork)
        {
            this.MaterialDetailRepository = MaterialDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateMaterialDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateMaterialDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateMaterialDetailHandler : ICommandHandler<CreateOrUpdateMaterialDetailCommand>
    {
        private readonly IMaterialDetailRepository MaterialDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateMaterialDetailHandler(IMaterialDetailRepository MaterialDetailRepository, IUnitOfWork unitOfWork)
        {
            this.MaterialDetailRepository = MaterialDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateMaterialDetailCommand command)
        {
            var MaterialDetail = new MaterialDetail
            {
                ID = command.ID,
                Code = command.Code,
                UOMID = command.UOMID,
                ProductModelID = command.ProductModelID,
                Quantity = command.Quantity,
                MaterialFormulaID=command.MaterialFormulaID,
                MaterialID=command.MaterialID,
                OrderID=command.OrderID,
                BillDetailID=command.BillDetailID,
                Price=command.Price
            };
            if (MaterialDetail.ID == 0)
                MaterialDetailRepository.Add(MaterialDetail);
            else
                MaterialDetailRepository.Update(MaterialDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateMaterialDetailCommand> commands)
        {
            List<MaterialDetail> list = new List<MaterialDetail>();
            foreach (var item in commands)
            {
                var company = new MaterialDetail
                {
                    ID = item.ID,
                    Code = item.Code,
                    ProductModelID = item.ProductModelID,
                    Quantity = item.Quantity,
                    UOMID = item.UOMID,
                    MaterialFormulaID = item.MaterialFormulaID,
                    MaterialID = item.MaterialID,
                    OrderID = item.OrderID,
                    BillDetailID = item.BillDetailID,
                    Price = item.Price

                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                MaterialDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                MaterialDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateMaterialDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            MaterialDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteMaterialDetailHandler : ICommandHandler<DeleteMaterialDetailCommand>
    {
        private readonly IMaterialDetailRepository MaterialDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteMaterialDetailHandler(IMaterialDetailRepository MaterialDetailRepository, IUnitOfWork unitOfWork)
        {
            this.MaterialDetailRepository = MaterialDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteMaterialDetailCommand command)
        {
            var MaterialDetail = MaterialDetailRepository.GetById(command.ID);
            MaterialDetailRepository.Delete(MaterialDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteMaterialDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            MaterialDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteMaterialDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //LabourDetail
    //Dung de check add
    public class CanAddLabourDetail : IValidationHandler<CreateOrUpdateLabourDetailCommand>
    {
        private readonly ILabourDetailRepository LabourDetailRepository;
        public CanAddLabourDetail(ILabourDetailRepository LabourDetailRepository, IUnitOfWork unitOfWork)
        {
            this.LabourDetailRepository = LabourDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateLabourDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateLabourDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateLabourDetailHandler : ICommandHandler<CreateOrUpdateLabourDetailCommand>
    {
        private readonly ILabourDetailRepository LabourDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateLabourDetailHandler(ILabourDetailRepository LabourDetailRepository, IUnitOfWork unitOfWork)
        {
            this.LabourDetailRepository = LabourDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateLabourDetailCommand command)
        {
            var LabourDetail = new LabourDetail
            {
                ID = command.ID,
                Code = command.Code,
                UOMID = command.UOMID,
                ProductModelID = command.ProductModelID,
                Quantity = command.Quantity,
                OrderID = command.OrderID,
                LevelID=command.LevelID,
                LabourFormulaID=command.LabourFormulaID,
                LevelNormID=command.LevelNormID,
                Norm=command.Norm,
                UOMID2=command.UOMID2,

            };
            if (LabourDetail.ID == 0)
                LabourDetailRepository.Add(LabourDetail);
            else
                LabourDetailRepository.Update(LabourDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateLabourDetailCommand> commands)
        {
            List<LabourDetail> list = new List<LabourDetail>();
            foreach (var item in commands)
            {
                var company = new LabourDetail
                {
                    ID = item.ID,
                    Code = item.Code,
                    ProductModelID = item.ProductModelID,
                    Quantity = item.Quantity,
                    UOMID = item.UOMID,
                    OrderID = item.OrderID,
                    LevelID = item.LevelID,
                    LabourFormulaID = item.LabourFormulaID,
                    LevelNormID = item.LevelNormID,
                    Norm = item.Norm,
                    UOMID2 = item.UOMID2,

                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                LabourDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                LabourDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateLabourDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            LabourDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteLabourDetailHandler : ICommandHandler<DeleteLabourDetailCommand>
    {
        private readonly ILabourDetailRepository LabourDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteLabourDetailHandler(ILabourDetailRepository LabourDetailRepository, IUnitOfWork unitOfWork)
        {
            this.LabourDetailRepository = LabourDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteLabourDetailCommand command)
        {
            var LabourDetail = LabourDetailRepository.GetById(command.ID);
            LabourDetailRepository.Delete(LabourDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteLabourDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            LabourDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteLabourDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //ToolDetail
    //Dung de check add
    public class CanAddToolDetail : IValidationHandler<CreateOrUpdateToolDetailCommand>
    {
        private readonly IToolDetailRepository ToolDetailRepository;
        public CanAddToolDetail(IToolDetailRepository ToolDetailRepository, IUnitOfWork unitOfWork)
        {
            this.ToolDetailRepository = ToolDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateToolDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateToolDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateToolDetailHandler : ICommandHandler<CreateOrUpdateToolDetailCommand>
    {
        private readonly IToolDetailRepository ToolDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateToolDetailHandler(IToolDetailRepository ToolDetailRepository, IUnitOfWork unitOfWork)
        {
            this.ToolDetailRepository = ToolDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateToolDetailCommand command)
        {
            var ToolDetail = new ToolDetail
            {
                ID = command.ID,
                Code = command.Code,
                ProductModelID = command.ProductModelID,
                Quantity = command.Quantity,
                OrderID = command.OrderID,
                UOM1ID=command.UOM1ID,
                Norm = command.Norm,
                UOMID2 = command.UOMID2,
                ToolFormulaID=command.ToolFormulaID,
                ToolNormID=command.ToolNormID,
                ToolTypeID=command.ToolTypeID,

            };
            if (ToolDetail.ID == 0)
                ToolDetailRepository.Add(ToolDetail);
            else
                ToolDetailRepository.Update(ToolDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateToolDetailCommand> commands)
        {
            List<ToolDetail> list = new List<ToolDetail>();
            foreach (var item in commands)
            {
                var company = new ToolDetail
                {
                    ID = item.ID,
                    Code = item.Code,
                    ProductModelID = item.ProductModelID,
                    Quantity = item.Quantity,
                    OrderID = item.OrderID,
                    Norm = item.Norm,
                    UOMID2 = item.UOMID2,
                    UOM1ID = item.UOM1ID,
                    ToolFormulaID = item.ToolFormulaID,
                    ToolNormID = item.ToolNormID,
                    ToolTypeID = item.ToolTypeID,

                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                ToolDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                ToolDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateToolDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            ToolDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteToolDetailHandler : ICommandHandler<DeleteToolDetailCommand>
    {
        private readonly IToolDetailRepository ToolDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteToolDetailHandler(IToolDetailRepository ToolDetailRepository, IUnitOfWork unitOfWork)
        {
            this.ToolDetailRepository = ToolDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteToolDetailCommand command)
        {
            var ToolDetail = ToolDetailRepository.GetById(command.ID);
            ToolDetailRepository.Delete(ToolDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteToolDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            ToolDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteToolDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //OtherCostDetail
    //Dung de check add
    public class CanAddOtherCostDetail : IValidationHandler<CreateOrUpdateOtherCostDetailCommand>
    {
        private readonly IOtherCostDetailRepository OtherCostDetailRepository;
        public CanAddOtherCostDetail(IOtherCostDetailRepository OtherCostDetailRepository, IUnitOfWork unitOfWork)
        {
            this.OtherCostDetailRepository = OtherCostDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateOtherCostDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateOtherCostDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateOtherCostDetailHandler : ICommandHandler<CreateOrUpdateOtherCostDetailCommand>
    {
        private readonly IOtherCostDetailRepository OtherCostDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateOtherCostDetailHandler(IOtherCostDetailRepository OtherCostDetailRepository, IUnitOfWork unitOfWork)
        {
            this.OtherCostDetailRepository = OtherCostDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateOtherCostDetailCommand command)
        {
            var OtherCostDetail = new OtherCostDetail
            {
                ID = command.ID,
                Code = command.Code,
                ProductModelID = command.ProductModelID,
                Quantity = command.Quantity,
                OrderID = command.OrderID,
                OtherCostID=command.OtherCostID,
                OtherCostFormulaID=command.OtherCostFormulaID,
                Norm=command.Norm,
                UOMID=command.UOMID

            };
            if (OtherCostDetail.ID == 0)
                OtherCostDetailRepository.Add(OtherCostDetail);
            else
                OtherCostDetailRepository.Update(OtherCostDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateOtherCostDetailCommand> commands)
        {
            List<OtherCostDetail> list = new List<OtherCostDetail>();
            foreach (var item in commands)
            {
                var company = new OtherCostDetail
                {
                    ID = item.ID,
                    Code = item.Code,
                    ProductModelID = item.ProductModelID,
                    Quantity = item.Quantity,
                    OrderID = item.OrderID,
                    Norm = item.Norm,
                    OtherCostID = item.OtherCostID,
                    OtherCostFormulaID = item.OtherCostFormulaID,
                    UOMID = item.UOMID

                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                OtherCostDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                OtherCostDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateOtherCostDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            OtherCostDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteOtherCostDetailHandler : ICommandHandler<DeleteOtherCostDetailCommand>
    {
        private readonly IOtherCostDetailRepository OtherCostDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteOtherCostDetailHandler(IOtherCostDetailRepository OtherCostDetailRepository, IUnitOfWork unitOfWork)
        {
            this.OtherCostDetailRepository = OtherCostDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteOtherCostDetailCommand command)
        {
            var OtherCostDetail = OtherCostDetailRepository.GetById(command.ID);
            OtherCostDetailRepository.Delete(OtherCostDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteOtherCostDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            OtherCostDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteOtherCostDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //*Dung cho thuc hien san xuat
    // OrderProcess
    public class CanAddOrderProcess : IValidationHandler<CreateOrUpdateOrderProcessCommand>
    {
        private readonly IOrderProcessRepository OrderProcessRepository;
        public CanAddOrderProcess(IOrderProcessRepository OrderProcessRepository, IUnitOfWork unitOfWork)
        {
            this.OrderProcessRepository = OrderProcessRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateOrderProcessCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateOrderProcessCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateOrderProcessHandler : ICommandHandler<CreateOrUpdateOrderProcessCommand>
    {
        private readonly IOrderProcessRepository OrderProcessRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateOrderProcessHandler(IOrderProcessRepository OrderProcessRepository, IUnitOfWork unitOfWork)
        {
            this.OrderProcessRepository = OrderProcessRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateOrderProcessCommand command)
        {
            var OrderProcess = new OrderProcess
            {
                ID = command.ID,
                StartDate = command.StartDate,
                EndDate = command.EndDate,
                ProductModelID = command.ProductModelID,
                Quantity = command.Quantity,
                Note = command.Note,
                Code = command.Code,
                OrderID=command.OrderID,
                Status=command.Status
            };
            if (OrderProcess.ID == 0)
                OrderProcessRepository.Add(OrderProcess);
            else
                OrderProcessRepository.Update(OrderProcess);

            unitOfWork.Commit();
            return new CommandResult(true, OrderProcess.ID);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateOrderProcessCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateOrderProcessCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteOrderProcessHandler : ICommandHandler<DeleteOrderProcessCommand>
    {
        private readonly IOrderProcessRepository OrderProcessRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteOrderProcessHandler(IOrderProcessRepository OrderProcessRepository, IUnitOfWork unitOfWork)
        {
            this.OrderProcessRepository = OrderProcessRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteOrderProcessCommand command)
        {
            var OrderProcess = OrderProcessRepository.GetById(command.ID);
            OrderProcessRepository.Delete(OrderProcess);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteOrderProcessCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            OrderProcessRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteOrderProcessCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //SubprocessDetail
    //Dung de check add
    public class CanAddSubprocessDetail : IValidationHandler<CreateOrUpdateSubprocessDetailCommand>
    {
        private readonly ISubprocessDetailRepository SubprocessDetailRepository;
        public CanAddSubprocessDetail(ISubprocessDetailRepository SubprocessDetailRepository, IUnitOfWork unitOfWork)
        {
            this.SubprocessDetailRepository = SubprocessDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateSubprocessDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateSubprocessDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateSubprocessDetailHandler : ICommandHandler<CreateOrUpdateSubprocessDetailCommand>
    {
        private readonly ISubprocessDetailRepository SubprocessDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateSubprocessDetailHandler(ISubprocessDetailRepository SubprocessDetailRepository, IUnitOfWork unitOfWork)
        {
            this.SubprocessDetailRepository = SubprocessDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateSubprocessDetailCommand command)
        {
            var SubprocessDetail = new SubprocessDetail
            {
                ID = command.ID,
                DesignDetailID=command.DesignDetailID,
                EndDate=command.EndDate,
                ExaminerID=command.ExaminerID,
                OrderProcessID=command.OrderProcessID,
                Quantity=command.Quantity,
                ResponsibleID=command.ResponsibleID,
                StartDate=command.StartDate,
                State=command.State,
                UnitID=command.UnitID,
                WorkplaceID=command.WorkplaceID,
                Evaluation1=command.Evaluation1,
                Evaluation2=command.Evaluation2

            };
            if (SubprocessDetail.ID == 0)
                SubprocessDetailRepository.Add(SubprocessDetail);
            else
                SubprocessDetailRepository.Update(SubprocessDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateSubprocessDetailCommand> commands)
        {
            List<SubprocessDetail> list = new List<SubprocessDetail>();
            foreach (var item in commands)
            {
                var company = new SubprocessDetail
                {
                    ID = item.ID,
                    DesignDetailID = item.DesignDetailID,
                    EndDate = item.EndDate,
                    ExaminerID = item.ExaminerID,
                    OrderProcessID = item.OrderProcessID,
                    Quantity = item.Quantity,
                    ResponsibleID = item.ResponsibleID,
                    StartDate = item.StartDate,
                    State = item.State,
                    UnitID = item.UnitID,
                    WorkplaceID = item.WorkplaceID,
                    Evaluation1 = item.Evaluation1,
                    Evaluation2 = item.Evaluation2

                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                SubprocessDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                SubprocessDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateSubprocessDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            SubprocessDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteSubprocessDetailHandler : ICommandHandler<DeleteSubprocessDetailCommand>
    {
        private readonly ISubprocessDetailRepository SubprocessDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteSubprocessDetailHandler(ISubprocessDetailRepository SubprocessDetailRepository, IUnitOfWork unitOfWork)
        {
            this.SubprocessDetailRepository = SubprocessDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteSubprocessDetailCommand command)
        {
            var SubprocessDetail = SubprocessDetailRepository.GetById(command.ID);
            SubprocessDetailRepository.Delete(SubprocessDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteSubprocessDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            SubprocessDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteSubprocessDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //CostDetail
    //Dung de check add
    public class CanAddCostDetail : IValidationHandler<CreateOrUpdateCostDetailCommand>
    {
        private readonly ICostDetailRepository CostDetailRepository;
        public CanAddCostDetail(ICostDetailRepository CostDetailRepository, IUnitOfWork unitOfWork)
        {
            this.CostDetailRepository = CostDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateCostDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateCostDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateCostDetailHandler : ICommandHandler<CreateOrUpdateCostDetailCommand>
    {
        private readonly ICostDetailRepository CostDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateCostDetailHandler(ICostDetailRepository CostDetailRepository, IUnitOfWork unitOfWork)
        {
            this.CostDetailRepository = CostDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateCostDetailCommand command)
        {
            var CostDetail = new CostDetail
            {
                ID = command.ID,
                DesignDetailID = command.DesignDetailID,
                OrderProcessID = command.OrderProcessID,
                LabourCost=command.LabourCost,
                MaterialCost=command.MaterialCost,
                OtherCost=command.OtherCost,
                ToolCost=command.ToolCost,
                TotalCost=command.TotalCost,

            };
            if (CostDetail.ID == 0)
                CostDetailRepository.Add(CostDetail);
            else
                CostDetailRepository.Update(CostDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateCostDetailCommand> commands)
        {
            List<CostDetail> list = new List<CostDetail>();
            foreach (var item in commands)
            {
                var company = new CostDetail
                {
                    ID = item.ID,
                    DesignDetailID = item.DesignDetailID,
                    OrderProcessID = item.OrderProcessID,
                    LabourCost = item.LabourCost,
                    MaterialCost = item.MaterialCost,
                    OtherCost = item.OtherCost,
                    ToolCost = item.ToolCost,
                    TotalCost = item.TotalCost,
                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                CostDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                CostDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateCostDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            CostDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteCostDetailHandler : ICommandHandler<DeleteCostDetailCommand>
    {
        private readonly ICostDetailRepository CostDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteCostDetailHandler(ICostDetailRepository CostDetailRepository, IUnitOfWork unitOfWork)
        {
            this.CostDetailRepository = CostDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteCostDetailCommand command)
        {
            var CostDetail = CostDetailRepository.GetById(command.ID);
            CostDetailRepository.Delete(CostDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteCostDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            CostDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteCostDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //*Dung cho ket thuc san xuat
    // OrderSummary
    public class CanAddOrderSummary : IValidationHandler<CreateOrUpdateOrderSummaryCommand>
    {
        private readonly IOrderSummaryRepository OrderSummaryRepository;
        public CanAddOrderSummary(IOrderSummaryRepository OrderSummaryRepository, IUnitOfWork unitOfWork)
        {
            this.OrderSummaryRepository = OrderSummaryRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateOrderSummaryCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateOrderSummaryCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateOrderSummaryHandler : ICommandHandler<CreateOrUpdateOrderSummaryCommand>
    {
        private readonly IOrderSummaryRepository OrderSummaryRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateOrderSummaryHandler(IOrderSummaryRepository OrderSummaryRepository, IUnitOfWork unitOfWork)
        {
            this.OrderSummaryRepository = OrderSummaryRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateOrderSummaryCommand command)
        {
            var OrderSummary = new OrderSummary
            {
                ID = command.ID,
                StartDate = command.StartDate,
                EndDate = command.EndDate,
                ProductModelID = command.ProductModelID,
                Quantity = command.Quantity,
                Note = command.Note,
                Code = command.Code,
                OrderID = command.OrderID,
                TotalCost=command.TotalCost,
                UnitPrice=command.UnitPrice,
                Evaluation1=command.Evaluation1,
                Evaluation2=command.Evaluation2,
                ExaminerID=command.ExaminerID,
                Description=command.Description,
                OrderProcessID=command.OrderProcessID,
                Status=command.Status
            };
            if (OrderSummary.ID == 0)
                OrderSummaryRepository.Add(OrderSummary);
            else
                OrderSummaryRepository.Update(OrderSummary);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateOrderSummaryCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateOrderSummaryCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de delete
    public class DeleteOrderSummaryHandler : ICommandHandler<DeleteOrderSummaryCommand>
    {
        private readonly IOrderSummaryRepository OrderSummaryRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteOrderSummaryHandler(IOrderSummaryRepository OrderSummaryRepository, IUnitOfWork unitOfWork)
        {
            this.OrderSummaryRepository = OrderSummaryRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteOrderSummaryCommand command)
        {
            var OrderSummary = OrderSummaryRepository.GetById(command.ID);
            OrderSummaryRepository.Delete(OrderSummary);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteOrderSummaryCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            OrderSummaryRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteOrderSummaryCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //StorageDetail
    //Dung de check add
    public class CanAddStorageDetail : IValidationHandler<CreateOrUpdateStorageDetailCommand>
    {
        private readonly IStorageDetailRepository StorageDetailRepository;
        public CanAddStorageDetail(IStorageDetailRepository StorageDetailRepository, IUnitOfWork unitOfWork)
        {
            this.StorageDetailRepository = StorageDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateStorageDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateStorageDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateStorageDetailHandler : ICommandHandler<CreateOrUpdateStorageDetailCommand>
    {
        private readonly IStorageDetailRepository StorageDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateStorageDetailHandler(IStorageDetailRepository StorageDetailRepository, IUnitOfWork unitOfWork)
        {
            this.StorageDetailRepository = StorageDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateStorageDetailCommand command)
        {
            var StorageDetail = new StorageDetail
            {
                ID = command.ID,
                OrderSummaryID=command.OrderSummaryID,
                ProductModelID=command.ProductModelID,
                Quantity=command.Quantity,
                WarehouseID=command.WarehouseID,
                Employee1ID=command.Employee1ID,
                Employee2ID=command.Employee2ID,
                Employee3ID=command.Employee3ID,
                DateOut=command.DateOut,
                BillNumber=command.BillNumber,
                

            };
            if (StorageDetail.ID == 0)
                StorageDetailRepository.Add(StorageDetail);
            else
                StorageDetailRepository.Update(StorageDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateStorageDetailCommand> commands)
        {
            List<StorageDetail> list = new List<StorageDetail>();
            foreach (var item in commands)
            {
                var company = new StorageDetail
                {
                    ID = item.ID,
                    OrderSummaryID = item.OrderSummaryID,
                    ProductModelID = item.ProductModelID,
                    Quantity = item.Quantity,
                    WarehouseID = item.WarehouseID,
                    Employee1ID = item.Employee1ID,
                    Employee2ID = item.Employee2ID,
                    Employee3ID = item.Employee3ID,
                    DateOut = item.DateOut,
                    BillNumber = item.BillNumber,

                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                StorageDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                StorageDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateStorageDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            StorageDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteStorageDetailHandler : ICommandHandler<DeleteStorageDetailCommand>
    {
        private readonly IStorageDetailRepository StorageDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteStorageDetailHandler(IStorageDetailRepository StorageDetailRepository, IUnitOfWork unitOfWork)
        {
            this.StorageDetailRepository = StorageDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteStorageDetailCommand command)
        {
            var StorageDetail = StorageDetailRepository.GetById(command.ID);
            StorageDetailRepository.Delete(StorageDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteStorageDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            StorageDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteStorageDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
    //BillOut
    //Dung de check add
    public class CanAddBillOut : IValidationHandler<CreateOrUpdateBillOutCommand>
    {
        private readonly IBillOutRepository BillOutRepository;
        public CanAddBillOut(IBillOutRepository BillOutRepository, IUnitOfWork unitOfWork)
        {
            this.BillOutRepository = BillOutRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateBillOutCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.Code == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateBillOutCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateBillOutHandler : ICommandHandler<CreateOrUpdateBillOutCommand>
    {
        private readonly IBillOutRepository BillOutRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateBillOutHandler(IBillOutRepository BillOutRepository, IUnitOfWork unitOfWork)
        {
            this.BillOutRepository = BillOutRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateBillOutCommand command)
        {
            var BillOut = new BillOut
            {
                ID = command.ID,
                WarehouseID=command.WarehouseID,
                User1ID=command.User1ID,
                User2ID=command.User2ID,
                Code=command.Code,
                Date1=command.Date1,
                Date2=command.Date2,
                Description=command.Description,
                TotalMoney=command.TotalMoney
            };
            if (BillOut.ID == 0)
                BillOutRepository.Add(BillOut);
            else
                BillOutRepository.Update(BillOut);

            unitOfWork.Commit();
            return new CommandResult(true,BillOut.ID);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateBillOutCommand> commands)
        {
            List<BillOut> list = new List<BillOut>();
            foreach (var item in commands)
            {
                var company = new BillOut
                {
                    ID = item.ID,

                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                BillOutRepository.AddRange(list.Where(x => x.ID == 0));
            else
                BillOutRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateBillOutCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            BillOutRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteBillOutHandler : ICommandHandler<DeleteBillOutCommand>
    {
        private readonly IBillOutRepository BillOutRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteBillOutHandler(IBillOutRepository BillOutRepository, IUnitOfWork unitOfWork)
        {
            this.BillOutRepository = BillOutRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteBillOutCommand command)
        {
            var BillOut = BillOutRepository.GetById(command.ID);
            BillOutRepository.Delete(BillOut);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteBillOutCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            BillOutRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteBillOutCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //BillDetail 
    //Dung de check add
    public class CanAddBillDetail : IValidationHandler<CreateOrUpdateBillDetailCommand>
    {
        private readonly IBillDetailRepository BillDetailRepository;
        public CanAddBillDetail(IBillDetailRepository BillDetailRepository, IUnitOfWork unitOfWork)
        {
            this.BillDetailRepository = BillDetailRepository;
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateBillDetailCommand command)
        {
            //TODO
            //Neu Atributes (System.ComponentModel.DataAnnotations) khong thoa man cac dieu kien validation thi ban co
            //the them cac validation o day. Vd:
            if (command.ID == null)
                yield return new ValidationResult("Name", string.Format(Resources.Name));
        }
        public IEnumerable<ValidationResult> ValidateRange(IEnumerable<CreateOrUpdateBillDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }

    //Dung de insert
    public class CreateOrUpdateBillDetailHandler : ICommandHandler<CreateOrUpdateBillDetailCommand>
    {
        private readonly IBillDetailRepository BillDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateBillDetailHandler(IBillDetailRepository BillDetailRepository, IUnitOfWork unitOfWork)
        {
            this.BillDetailRepository = BillDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateBillDetailCommand command)
        {
            var BillDetail = new BillDetail
            {
                ID = command.ID,
                BillOutID=command.BillOutID,
                Maintenancecost=command.Maintenancecost,
                MaterialID=command.MaterialID,
                Price_In=command.Price_In,
                Price_Out=command.Price_Out,
                PurchaseDetailID=command.PurchaseDetailID,
                Quantity=command.Quantity,
                UOMID=command.UOMID,
                TotalMoney = command.TotalMoney,
            };
            if (BillDetail.ID == 0)
                BillDetailRepository.Add(BillDetail);
            else
                BillDetailRepository.Update(BillDetail);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateBillDetailCommand> commands)
        {
            List<BillDetail> list = new List<BillDetail>();
            foreach (var item in commands)
            {
                var company = new BillDetail
                {
                    ID = item.ID,
                    BillOutID = item.BillOutID,
                    Maintenancecost = item.Maintenancecost,
                    MaterialID = item.MaterialID,
                    Price_In = item.Price_In,
                    Price_Out = item.Price_Out,
                    PurchaseDetailID = item.PurchaseDetailID,
                    Quantity = item.Quantity,
                    UOMID = item.UOMID,
                    TotalMoney = item.TotalMoney,
                };
                list.Add(company);
            }
            if (list.Any(x => x.ID == 0))
                BillDetailRepository.AddRange(list.Where(x => x.ID == 0));
            else
                BillDetailRepository.UpdateRange(list.Where(x => x.ID != 0));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateBillDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            BillDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }

    //Dung de delete
    public class DeleteBillDetailHandler : ICommandHandler<DeleteBillDetailCommand>
    {
        private readonly IBillDetailRepository BillDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public DeleteBillDetailHandler(IBillDetailRepository BillDetailRepository, IUnitOfWork unitOfWork)
        {
            this.BillDetailRepository = BillDetailRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(DeleteBillDetailCommand command)
        {
            var BillDetail = BillDetailRepository.GetById(command.ID);
            BillDetailRepository.Delete(BillDetail);
            unitOfWork.Commit();
            return new CommandResult(true);
        }


        public ICommandResult ExecuteRange(IEnumerable<DeleteBillDetailCommand> commands)
        {
            var ids = commands.Select(y => y.ID);
            BillDetailRepository.Delete(x => ids.Contains(x.ID));
            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult DeleteRange(IEnumerable<DeleteBillDetailCommand> commands)
        {
            throw new NotImplementedException();
        }
    }


}

